package com.wsh.controller.sys;

import com.wsh.dto.sys.MenuDto;
import com.wsh.utils.JsonResult;
import com.wsh.base.BaseVo;
import com.wsh.entity.sys.MenuEntity;
import com.wsh.vo.sys.MenuVo;
import com.wsh.sys.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @Author 王思涵
 * @Date 2019/7/21
 * @Version 1.0
 * @Desciption 菜单数据接口
 **/
@RestController
@RequestMapping("/sys/menu")
@Api(tags = "系统：菜单管理")
public class MenuController {

    /**
     * 菜单业务
     */
    @Autowired
    private MenuService menuService;

    @GetMapping("/getTree")
    public Object getTree() {
        return JsonResult.success(menuService.getRoleTree("0"));
    }

    @ApiOperation("菜单查询")
    @PreAuthorize("@el.check('menu:list')")
    @GetMapping("/getMenuAll")
    public Object getMenuAll(MenuDto menuDto) {
        return JsonResult.success(menuService.getList(menuDto));
    }

    @ApiOperation("保存菜单")
    @PostMapping("/saveMenu")
    @PreAuthorize("@el.check('menu:save')")
    public Object saveMenu(@RequestBody MenuVo menuVo) {
         MenuEntity menuEntity = new MenuEntity();
         BeanUtils.copyProperties(menuVo, menuEntity);
        if (StringUtils.isEmpty(menuEntity.getMenuId())) {
            menuEntity.setCreateTime(new Date());
        }
        menuEntity.setUpdateTime(new Date());
        return JsonResult.success(menuService.saveOrUpdate(menuEntity));
    }

    @ApiOperation("菜单删除")
    @PreAuthorize("@el.check('menu:del')")
    @PostMapping("/delete")
    public Object delete(String menuId) {
        return JsonResult.success(menuService.deleteMenuTree(menuId));
    }

    @GetMapping("/getMenuById")
    public Object getMenuById(@RequestParam String formId) { return JsonResult.success(new BaseVo(menuService.getById(formId))); }
}
